<!DOCTYPE html>
<!--
Copyright 2018 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/metrics/rendering/queueing_duration.html">
<link rel="import" href="/tracing/model/user_model/segment.html">
<link rel="import" href="/tracing/value/histogram_set.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  test('queueingDurations', function() {
    const model = tr.c.TestUtils.newModel((model) => {
      // Metric computation assumes that there is always a browser process.
      model.getOrCreateProcess(0).getOrCreateThread(0).name = 'CrBrowserMain';

      const compositor = model.getOrCreateProcess(1).getOrCreateThread(1);
      compositor.name = 'Compositor';
      compositor.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx(
          { title: 'ThreadProxy::ScheduledActionSendBeginMainFrame',
            start: 3, end: 3,
            args: { begin_frame_id: 2 }}));
      compositor.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx(
          { title: 'ThreadProxy::ScheduledActionSendBeginMainFrame',
            start: 4, end: 4,
            args: { begin_frame_id: 1 }}));

      const main = model.getOrCreateProcess(1).getOrCreateThread(0);
      main.name = 'CrRendererMain';
      main.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx(
          { title: 'ThreadProxy::BeginMainFrame',
            start: 5, end: 5,
            args: { begin_frame_id: 1 }}));
      main.sliceGroup.pushSlice(tr.c.TestUtils.newSliceEx(
          { title: 'ThreadProxy::BeginMainFrame',
            start: 6, end: 6,
            args: { begin_frame_id: 2 }}));
    });
    const histograms = new tr.v.HistogramSet();
    tr.metrics.rendering.addQueueingDurationHistograms(
        histograms, model, [new tr.model.um.Segment(0, 10)]);

    const hist = histograms.getHistogramNamed('queueing_durations');
    assert.closeTo(1, hist.min, 1e-6);
    assert.closeTo(3, hist.max, 1e-6);
    assert.closeTo(2, hist.average, 1e-6);
  });
});
</script>
